#include <stdio.h>
#include <malloc.h>
#include <assert.h>


//动态数据表典型定义
typedef struct {
    int *arr;//存储数组的指针
    int size;
    int capacity;
}SL;

//初始化
void init(SL *p){
    p->arr=NULL;
    p->size=0;
    p->capacity=0;
}

//销毁动态IP的内存空间
void destory(SL *p){
    free(p->arr);
    p->arr=NULL;
    p->size=0;
    p->capacity=0;
}
//扩容
void checkCapacity(SL *p){
    if(p->size==p->capacity){
        int  newCapacity=p>0?4:2*p->capacity;
        int *temp= realloc(p->arr,sizeof (int )*newCapacity);
        if(temp==NULL){
            printf("扩容失败");
        }
        p->arr=temp;
        p->capacity=newCapacity;
        printf("%d\n",p->capacity);

    }
}//打印数组
void SLprintf(SL *p){
    for(int i=0;i < p->size;++i){
        printf("%d\n",p->arr[i]);
    }
}

void punBack(SL*p,int  n){
    checkCapacity(p);
    p->arr[p->size++]=n;
}
void pushFront(SL *P,int n){
    checkCapacity(P);
    for (int i = P->size; i>0; i--) {
        P->arr[i]=P->arr[i-1];
    }
    P->arr[0]=n;
    P->size++;
}

//在任意位置插入
void insert(SL *P,int pos,int n){
    if(pos>=0&&pos<=P->size){
        checkCapacity(P);
        for (int i = P->size; i >pos ; i--) {
            P->arr[i]=P->arr[i-1];
        }
        P->arr[pos]=n;
        P->size++;
    } else{
        printf("插入位置的元素应该在顺序表的范围内");
    }
}
//删除表头
void deleFor(SL *P){
    for (int i = 0; i < P->size-1; ++i) {
        P->arr[i]=P->arr[i+1];
    }
    P->size--;
}
//删除任意位置元素
void del(SL *P,int pos){
    assert(pos>=0&&pos<=P->size);
    for (int i = pos; i < P->size; ++i) {
        P->arr[i]=P->arr[i+1];
    }
    P->size--;
}
int main() {
    SL L;
    init(&L);
    checkCapacity(&L);
    punBack(&L,15);
    pushFront(&L,12);
    insert(&L,1,23);
    deleFor(&L);
    del(&L,2);
    SLprintf(&L);
    return 0;
}